package auth

import (
	"context"
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware"
	"go-kratos-demo/internal/lib/consts"
	"go-kratos-demo/internal/lib/contextx"
	"strconv"
)

func ParseHeader(logger log.Logger) middleware.Middleware {

	return func(handler middleware.Handler) middleware.Handler {

		return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
			// 获取请求头
			header := contextx.NewHeader(ctx)

			if header.GetUID() != "" {
				if uId, err := strconv.ParseInt(header.GetUID(), 10, 32); err == nil {
					ctx = context.WithValue(ctx, consts.CtxUserIdKey, uId)
				}
			}

			return handler(ctx, req)

		}
	}
}
